<?php
declare (strict_types=1);

namespace app\admin\controller\system;

use app\admin\controller\CommonController;
use app\common\model\AclUser;
use think\db\BaseQuery;
use think\facade\Db;

class Log extends CommonController
{
    protected $alias = 'l';
    protected $createByField = false;
    protected $updateByField = false;

    protected function getBaseModel(): BaseQuery
    {
        $range = $this->request->param('l_create_time_range');
        if (!empty($range)) {
            [$beginTime, $endTime] = explode(' - ', $range);
            $month = date('Ym', strtotime($beginTime));
        } else {
            $month = date('Ym');
        }
        return Db::name('system_log_' . $month)->alias($this->alias)
            ->leftJoin(AclUser::MODEL_NAME . ' u', $this->alias . '.user_id=u.id');
    }

    protected function getSearchFields(): string
    {
        return 'l.id,u.username,l.method,l.url,l.fingerprint,l.content,l.ip,l.useragent,l.create_time';
    }

    protected function getExportHeader()
    {
        return [
            [lang('common.id'), 'id']
            , [lang('log.username'), 'username']
            , [lang('log.method'), 'method']
            , [lang('log.url'), 'url']
            , [lang('log.fingerprint'), 'fingerprint']
            , [lang('log.content'), 'content']
            , [lang('log.ip'), 'ip']
            , [lang('log.useragent'), 'useragent']
            , [lang('log.create_time'), 'create_time']
        ];
    }
}